using System.Collections.Generic;
using Microsoft.Xna.Framework.Input;
using Itdavany;
using Itdavany.Data;
using Itdavany.Utility;
//using Itdavany.
using RollingKitty.Objects;
using RollingKitty.Data;

namespace RollingKitty.Stages
{
    public class ScenarioViewStage : KittyStage
    {
        GameObjectGroup faderlayer = new GameObjectGroup();
        //ScreenFader fader; 
        StageClosedEventArgs exitargs = new StageClosedEventArgs();

        List<string> FrameToKey = new List<string>();
        int curframe = 0;

        FrameArray frms;

        public new Scene CurrentScene
        {
            get
            {
                return base.CurrentScene;
            }

            set
            {
                Scene nc = new Scene();
                nc.AddRange(value);
                nc.Add(faderlayer);
                base.CurrentScene = nc;
            }
        }

        public ScenarioViewStage(FrameArray frms, string exitcode)
            : base()
        {
            FaderObject = new ScreenFader(((Configuration)Shared.GlobalBackstageGroup["conf"]).programConfig.screenFader.texture);
            FaderObject.FadeSpeed = ((Configuration)Shared.GlobalBackstageGroup["conf"]).programConfig.screenFader.fadespeed;
            FaderObject.FadeOutOver += new System.EventHandler(OnFadeOutOver);

            faderlayer.Add("fader", FaderObject);

            exitargs.code = exitcode;
            FrameToKey.AddRange(frms.Keys);

            CurrentScene = frms[FrameToKey[0]];
            this.frms = frms;

            AudioHelper.PlayBGM(null);

            FadeInAndFreeze();
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (!FaderObject.BeingFading)
            {
                if (MouseInput.IsReleased(MouseButton.Left))
                {
                    if (++curframe >= FrameToKey.Count)
                    {
                        FadeOutAndFreeze();
                    }
                    else
                    {
                        CurrentScene = frms[FrameToKey[curframe]];
                    }
                }
            }

            base.Update(gameTime);
        }

        public override void Draw()
        {
            base.Draw();
        }

        public void OnFadeOutOver(object obj, System.EventArgs args)
        {
            RaiseClosed(exitargs);
        }
    }
}